<!DOCTYPE html>
<html lang="en">

<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>firstStringBetween</title>
</head>

<body>
    <div>打开浏览器控制台查看结果</div>
    <script>
        const firstStringBetween = (str, start, end) => {
            const startIdx = str.indexOf(start);
            const endIdx = str.indexOf(end, startIdx + 1);

            if (startIdx === -1 || endIdx === -1 || startIdx >= endIdx) {
                return '';
            }

            return str.slice(startIdx + 1, endIdx).trim();
        }
        const res = firstStringBetween('This is a [custom] string', '[', ']'); // custom
        console.log('当前提取的子字符串是:',res);
    </script>
</body>

</html>